#include <iostream>
#include <map>
#include <string>
#include <utility>

using std::cout;
using std::endl;
using std::multimap;
using std::string;
using std::make_pair;
using std::pair;

template <typename  Container>
void display(const Container &con)
{
    for(auto &elem : con)
    {
        cout << elem.first << "  " << elem.second << endl;
    }
}

void test()
{
    //multimap的基本特征
    //1、存放的是key-value类型，key值是不唯一的，可以重复；value值
    //可以相同，也可以不同
    //2、默认会按照key值进行升序排列
    //3、底层使用的是红黑树
    multimap<int, string> number = {
        pair<int, string>(4, "beijing"),//使用pair的临时对象
        pair<int, string>(4, "beijing"),
        make_pair(1, "wuhan"),//利用make_pair返回结果是pair类型
        make_pair(3, "wuhan"),
        {5, "hubei"},//利用大括号构建pair
        {6, "wangdao"},
    };
    display(number);

    cout << endl << "multimap的查找" << endl;
    size_t cnt = number.count(7);
    cout << "cnt = " << cnt << endl;
    multimap<int, string>::iterator it = number.find(8);
    if(it !=  number.end())
    {
        cout << "元素是存在 " 
             << it->first << "  " 
             << it->second << endl;
    }
    else
    {
        cout << "元素是不存在的" << endl;
    }

    cout << endl << "multimap的insert操作" << endl;
    /* number.insert({7, "nanjing"}); */
    /* number.insert(pair<int, string>(7, "nanjing")); */
    number.insert(make_pair(7, "nanjing"));
    display(number);

#if 0
    cout << endl <<endl;
    cout << "number[3] = " << number[3] << endl;//查找
    cout << "number[2] = " << number[2] << endl;//插入
    display(number);

    cout <<endl;
    //T &operator[](const Key &key)
    number[2] = "wangdao";//修改
    number[4] = "wangdao";
    display(number);
#endif

    /* const multimap<int, int> tmp = {{1, 2}}; */
    /* tmp[1];//error */
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

